package com.slz3.business.module.wmsManagement.wmsStockOutItems.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.wmsManagement.wmsStockOutItems.domain.WmsStockOutItems;
import com.slz3.business.module.wmsManagement.wmsStockOutItems.domain.WmsStockOutItemsFactory;
import com.slz3.business.module.wmsManagement.wmsStockOutItems.steward.WmsStockOutItemsSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class WmsStockOutItemsServiceImpl implements WmsStockOutItemsService {
    @Override
    public WmsStockOutItems save(WmsStockOutItems wmsStockOutItems) {
        return Repositorys.wmsStockOutItems.save(WmsStockOutItemsFactory.initBaseFields(wmsStockOutItems));
    }
    @Override
    public Page<WmsStockOutItems> page(MultiValueMap<String, String> params) {
        Page<WmsStockOutItems> wmsStockOutItemsPage = Repositorys.wmsStockOutItems.findAll(
                WmsStockOutItemsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return wmsStockOutItemsPage;
    }

    @Override
    public WmsStockOutItems modify(WmsStockOutItems wmsStockOutItemsNew) {
        return Repositorys.wmsStockOutItems.saveAndFlush(WmsStockOutItemsFactory.initBaseFields(wmsStockOutItemsNew));
    }

    @Override
    public Optional<WmsStockOutItems> single(UUID uuid) {
        return Repositorys.wmsStockOutItems.findById(uuid);
    }

    @Override
    public List<WmsStockOutItems> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsStockOutItems.findAllById(uuids);
    }

    @Override
    public List<WmsStockOutItems> delete(List<WmsStockOutItems> wmsStockOutItemsList) {
        wmsStockOutItemsList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsStockOutItemsFactory.initBaseFields(item);
        });
        return Services.wmsStockOutItemsTransactional.batchModify(wmsStockOutItemsList);
    }

}
